package com.jxt.myjxtmanager.views.popwindow;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.GridLayoutManager;
import android.text.TextUtils;
import android.text.format.Time;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AbsoluteLayout;
import android.widget.PopupWindow;

import com.jxt.myjxtmanager.R;
import com.jxt.myjxtmanager.utils.ScreenUtils;
import com.jxt.myjxtmanager.views.MySetMaxHeightRecyclerView;

import java.util.List;


/**
 * Created by qm on 2016/12/16 0016.
 */
public class MyPopRecyOneToSixView extends PopupWindow {
    private final PopRecyOneToSixAdapter pAdapter;
    private View contentView;
    private MySetMaxHeightRecyclerView lvResults;
    //分别定义两种显示格式的值
    private static int titleFlag = 1;
    private static int GridFlag = 3;
    private static int MonthFlag = 6;
    private boolean myisLink = false;
    private int beforeType = -1;
    private int currentType = -1;
    private String lastStr = "";

    //下拉列表项被单击的监听器
    private OnItemClickListener listener;

    /**
     * 使用Parcel在两个Activity中进行数据传送，Parcel需要实现Parcelable接口。
     * 而如果在Parcel类中，需要读或者写其他的bean数据的话，这些bean需要实现Serializable序列化接口。
     * 不然会出现Java.lang.RuntimeException: Parcel: unable to marshal value错误！
     *
     * @param activity
     * @param context
     * @param layoutInflater
     * @param oneToSixInfos
     * @param width
     * @param itemClickListener
     * @param isLink
     */
    public MyPopRecyOneToSixView(final Activity activity, final Context context, LayoutInflater layoutInflater,
                                 final List<PopRecyOneToSixInfo> oneToSixInfos, int width,
                                 OnItemClickListener itemClickListener, final boolean isLink) {
        super(context);
        this.listener = itemClickListener;
        this.myisLink = isLink;
        contentView = layoutInflater.inflate(R.layout.my_poprecy_one_to_six_list, null);

        lvResults = (MySetMaxHeightRecyclerView) contentView.findViewById(R.id.rcv_main);

        /**
         * LinearLayoutManager
         */
//        linearLayoutManager = new ItemGridLayoutManager(context, 4);
//        lvResults.setLayoutManager(linearLayoutManager);


        /**
         * 在这里，我们把LayoutManager设成Grid形式，根据实际情况，这里我设置成三列
         * setSpanSizeLookup方法，个人感觉有点像我们xml布局中的layout_weight,它表示Grid中的每项占几个位置
         * 在这里，根据实际情况，我们可以得知，类别栏和下面的list形式数据全是每条占满的也就是一个占三个的位置
         */
        GridLayoutManager gManager = new GridLayoutManager(context, 6);
        gManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                if (oneToSixInfos.get(position).getPosition() == 6) {
                    return 1;
                } else if (oneToSixInfos.get(position).getPosition() == 3) {
                    return 2;
                } else if (oneToSixInfos.get(position).getPosition() == 1) {
                    return 6;
                }
                return -1;
            }
        });
        lvResults.setRecyclHeight(ScreenUtils.getScreenH(activity) / 2);
        gManager.setAutoMeasureEnabled(true);
//        lvResults.setHasFixedSize(false);
        lvResults.setLayoutManager(gManager);
        pAdapter = new PopRecyOneToSixAdapter(oneToSixInfos, context);
        lvResults.setAdapter(pAdapter);
        final List<PopRecyOneToSixInfo> infos = oneToSixInfos;
        pAdapter.setOnItemClickLitener(new PopRecyOneToSixAdapter.OnItemClickLitener() {
            @Override
            public void OnItemClick(View view, int positon, int type) {
                if (listener != null) {
                    if (listener != null)
                        if (type == 1) {
//                            listener.onItemClick(view, infos.get(positon).getTitle(), positon, myisLink);
                        } else if (type == 3) {
                            currentType = 3;
                            listener.onItemClick(view, infos.get(positon).getThreeName(), positon, myisLink);
                            if (myisLink && beforeType != currentType) {
                                lastStr = infos.get(positon).getThreeName();
//                                myisLink = false;
//                                beforeType = currentType;
                            } else if (myisLink && beforeType == currentType) {
                                lastStr = infos.get(positon).getThreeName();
//                                myisLink = true;
//                                beforeType = currentType;
                            } else {
//                                beforeType = currentType;
                                dismiss();
                            }
                            pAdapter.notifyItemChanged(pAdapter.getSelectItem_One());
                            pAdapter.setSelectItem_One(positon);
                            pAdapter.notifyItemChanged(positon);
                        } else if (type == 6) {
                            currentType = 6;
                            if (myisLink) {
                                if (TextUtils.isEmpty(lastStr)) {
                                    Time time = new Time("GMT+8");
                                    time.setToNow();
                                    int year = time.year;
                                    listener.onItemClick(view, year + "-" + infos.get(positon).getSixName(), positon, false);
                                } else {
                                    listener.onItemClick(view, lastStr + "-" + infos.get(positon).getSixName(), positon, false);
                                }
                                pAdapter.setSelectItem_Two(positon);
                            } else {
                                listener.onItemClick(view, infos.get(positon).getSixName(), positon, false);
                                pAdapter.setSelectItem_One(positon);
                            }
                            beforeType = currentType;
                            dismiss();
                        }
                    beforeType = currentType;
                }
//                ToastUtil.showToast(context, infos.get(positon).getMsg() + "" + positon);
//                dismiss();
            }

            @Override
            public void OnItemLongClick(View view, int position) {

            }
        });

        //如果PopupWindow中的下拉列表项被单击了
        //则通知外部的下拉列表项单击监听器并传递当前单击项的数据
//        lvResults.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//            @Override
//            public void onItemClick(AdapterView<?> arg0, View view, int index, long arg3) {
//                if (listener != null) {
//                    if (listener != null)
//                        listener.onItemClick(view, ((TextView) view.findViewById(R.id.tvLocation))
//                                .getText().toString().trim(), index);
//                    dismiss();
//                }
//            }
//        });
        this.setContentView(contentView);  //设置悬浮窗体内显示的内容View
        this.setWidth(width);   //设置悬浮窗体的宽度
//        if (activity != null) {
//            this.setHeight(ScreenUtils.getScreenH(activity) / 2);
//        } else {
        this.setHeight(AbsoluteLayout.LayoutParams.MATCH_PARENT);
//        }
//        popupWindow.setWidth(LayoutParams.WRAP_CONTENT);
//        this.setHeight(AbsoluteLayout.LayoutParams.WRAP_CONTENT);
        //this.setWidth(LayoutParams.WRAP_CONTENT);
//        this.setHeight(300);   //设置悬浮窗体的高度
//        this.setBackgroundDrawable(new ColorDrawable(0x33000000)); // 设置悬浮窗体背景
        //this.setAnimationStyle(R.style.PopupAnimation);
//        this.setAnimationStyle(R.anim.popup_enter);     //设置悬浮窗体出现和退出时的动画
        this.setFocusable(true);    // menu菜单获得焦点 如果没有获得焦点menu菜单中的控件事件无法响应

        this.setOutsideTouchable(true);   //可以再外部点击隐藏掉PopupWindow
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0x00000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);
//        backgroundAlpha(activity, 0.5f);//0.0-1.0
        this.setOnDismissListener(new OnDismissListener() {

            @Override
            public void onDismiss() {
                // TODO Auto-generated method stub
                backgroundAlpha(activity, 1f);
            }
        });
    }

    public interface OnItemClickListener {
        void onItemClick(View view, String data, int position, boolean isLink);
    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(Activity context, float bgAlpha) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        context.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        context.getWindow().setAttributes(lp);
    }

    public void RefreshAdapterSelect() {
        if (pAdapter == null) {
        } else {
            pAdapter.notifyDataSetChanged();
        }
    }

    public void setDefultSelectBg() {
        pAdapter.setSelectItem_One(-1);
        pAdapter.setSelectItem_Two(-1);

    }
}
